<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="撞库时间">
        <el-date-picker
          v-model="tempDateRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd"
          class="small-date-picker"
          @change="handleDateChange" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="small"
          @click="handleAdd"
          v-hasPermi="['merchant:check:add']"
        >撞库</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="small"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['merchant:check:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['merchant:check:remove']"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkList" @selection-change="handleSelectionChange">
      <el-table-column label="商户id" align="center" prop="infoId" />
      <el-table-column label="商户名称" align="center" prop="infoName" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="姓名" align="center" prop="realName" />
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="响应结果" align="center" prop="message" />
      <el-table-column label="撞库时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="remarks">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.remarks || '暂无描述'"
          placement="top"
          effect="dark">

                    <el-button
                      size="small"
                      type="text"
                      @click="handleUpdate1(scope.row)"
                      v-hasPermi="['merchant:check:edit']">
                      <span>{{ scope.row.remarks || '暂无描述' }}</span>
                    </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="描述修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="small"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['merchant:check:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['merchant:check:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog :title="title" :visible.sync="opens" width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备注">
          <el-input
            type="text"
            v-model="form.remarks"
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm1">确定</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改撞库测试对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="机构名称" prop="infoId" class="single-line-label">
          <span class="label-spacing"></span>
          <el-select v-model="form.infoId" clearable placeholder="请选择机构名称" filterable>
            <el-option v-for="(item, i) in extData.infoIdList || []" :key="i" :label="`${item.key}-${item.value}`"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-select v-model="form.city" clearable placeholder="请选择城市" filterable>
            <el-option v-for="(item, i) in extData.cityList || []" :key="i" :label="``"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="备注" prop="remarks">-->
<!--          <el-input v-model="form.remarks" placeholder="请输入备注" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">撞 库</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCheck, getCheck, delCheck, addCheck, updateCheck,configInfoExtData } from "@/api/merchant/check";
import dayjs from "dayjs";

export default {
  name: "Check",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 撞库测试表格数据
      checkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opens: false,
      extData: {
        infoIdList:[]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mobile: null,
        createTime: null,
        updateTime: null,
        startDateInfo: null,
        endDateInfo: null,
      },
      tempDateRange: [],
      // 表单参数
      form: {
        infoId: null,
        remarks: '',
      },
      // 表单校验
      rules: {
        infoId: [
          { required: true, message: "商户id不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        realName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证不能为空", trigger: "blur" }
        ],
        city: [
          { required: true, message: "城市不能为空", trigger: "blur" }
        ],
        remarks: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getOptions();
    //this.setDefaultDateRange();
  },
  methods: {
    /** 查询撞库测试列表 */
    getList() {
      this.loading = true;
      listCheck(this.queryParams).then(response => {
        this.checkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    getOptions() {
      configInfoExtData().then((response) => {
        this.extData = response.data;
        console.log( this.extData)
      });
    },

    setDefaultDateRange() {
      const todayStart = dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss');
      const todayEnd = dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss');

      this.queryParams.startDateInfo = todayStart;
      this.queryParams.endDateInfo = todayEnd;
      this.tempDateRange = [todayStart, todayEnd];
    },
    handleDateChange(dateRange) {
      if (dateRange && dateRange.length === 2) {
        const startDate = dayjs(dateRange[0]).startOf('day').format('YYYY-MM-DD HH:mm:ss');
        const endDate = dayjs(dateRange[1]).endOf('day').format('YYYY-MM-DD HH:mm:ss');

        this.queryParams.startDateInfo = startDate;
        this.queryParams.endDateInfo = endDate;
      } else {
        this.queryParams.startDateInfo = null;
        this.queryParams.endDateInfo = null;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        infoId: null,
        infoName: null,
        mobile: null,
        realName: null,
        idCard: null,
        city: null,
        message: null,
        remarks: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.queryParams.dateRange = [];
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加撞库测试";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCheck(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改撞库测试";
      });
    },

    /** 修改按钮操作 */
    handleUpdate1(row) {
      const id = row.id
      getCheck(id).then(response => {
        this.form = response.data;
        this.opens = true;
        this.title = "修改备注信息";
      });
    },

    submitForm1() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCheck(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.opens = false;
              this.getList();
            });
          }
        }
      });
    },



    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCheck(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheck(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除撞库测试编号为"' + ids + '"的数据项？').then(function() {
        return delCheck(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

  }
};
</script>
